<?php

namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Auth;

class SessionController extends Controller
{
    //

    public function __construct()
    {
      # code...
      $this->middleware('guest',[
        'only'=>['create']
      ]);
    }
    public function create()
    {
      # code...

      return view('sessions.create');
    }
    public function destroy()
    {

      Auth::logout();
      session()->flash('success','你成功退出');
      return redirect('login');
    }

    public function store(Request  $request)
    {
      # code...

      $this->validate($request,[
        'email'=>'required|email|max:255',
        'password'=>'required',
      ]);

      $credentials=[
        'email' => $request->email,
        'password'=> $request->password,
      ];
      if(Auth::attempt($credentials,$request->has('remember'))){
        //登录成功
            if (Auth::user()->activated){
              session()->flash('success','欢迎回来');
              return redirect()->route('users.show',[Auth::user()]);
            }else{
              Auth::logout();
              session()->flash('warning','你的帐号未激活 请检查邮箱中的注册邮件激活');
              return redirect('/');
            }

      }else{
       session()->flash('danger','很抱歉 ，你的邮箱密码不匹配');
       return redirect()->back();

    }


    }
}
